import http from "@ohos.net.http";
import router from "@system.router";
import prompt from "@system.prompt";
import { isPasswordValid } from "../../common/js/utils.js";

export default {
    data: {
        password: "",
    },
    props: {
        // 从 forget1 传入
        phone: {
            default: "",
        },
        code: {
            default: "",
        },
    },
    goBack() {
        router.back();
    },
    onPasswordChange(ev) {
        this.password = ev.value;
    },
    confirm() {
        // 若密码规则错误或为空，则提示“请输入6~20位数字字母组合密码”。
        if (!isPasswordValid(this.password)) {
            prompt.showToast({
                message: "请输入6~20位数字字母组合密码",
            });
            return;
        }
        const req = http.createHttp();
        const promise = req.request("https://trezedo.club/api/ha/user/reset", {
            method: "POST",
            extraData: {
                code: this.code,
                phone: this.phone,
                newPassword: this.password,
            },
        });
        promise.then(({ result }) => {
            console.info("!! " + result);
            result = JSON.parse(result);
            // @ts-ignore
            if (result.code == 200) {
                // 用户修改密码成功时，返回登录页，并Toast提示“密码修改成功，请重新登录”
                router.push({
                    uri: "pages/login_main/login",
                    params: {
                        fromForget2: true,
                    },
                });
                router.clear();
                return;
            }
            prompt.showToast({
                // @ts-ignore
                message: result.msg,
            });
        });
    },
};
